package botoseis.mainGui.database;

import java.awt.Rectangle;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.jzy3d.chart.Chart;
import org.jzy3d.chart.ChartLauncher;
import org.jzy3d.colors.Color;
import org.jzy3d.colors.ColorMapper;
import org.jzy3d.colors.colormaps.ColorMapRainbow;
import org.jzy3d.global.Settings;
import org.jzy3d.maths.Coord3d;
import org.jzy3d.plot3d.primitives.MultiColorScatter;
import org.jzy3d.plot3d.rendering.legends.colorbars.ColorbarLegend;
import org.jzy3d.plot3d.rendering.view.modes.ViewPositionMode;

public class PlotScatter {
    Connection con=null;
    ResultSet rs = null;
    PreparedStatement pst = null;
    
 public PlotScatter(){        

        float x;
        float y;
        float z;
        float min;
        float max;
        
        con=DBConnect.ConnectDb();
             try {  
                   String queryPr ="SELECT  COUNT(*) as nrows FROM ATTRXYZ ";
                   pst=con.prepareStatement(queryPr);
                   rs = pst.executeQuery();
                   rs.next();
                   int size=rs.getInt("nrows");
                   
                   String queryMinMax ="SELECT MAX(Z1) AS max, MIN(Z1) AS min FROM ATTRXYZ";
                   pst=con.prepareStatement(queryMinMax);
                   rs = pst.executeQuery();
                   rs.next();
                   min=rs.getFloat("min");
                   max=rs.getFloat("max");
                   
                   String queryP ="SELECT  X1,Y1,Z1 FROM ATTRXYZ ";
                   pst=con.prepareStatement(queryP);
                   rs = pst.executeQuery();
                   int i=0;

                Coord3d[] points = new Coord3d[size];

                while (rs.next()) { 
                x =  (float) rs.getFloat("X1");
                y =  rs.getFloat("Y1");
                z = rs.getFloat("Z1");

                  points[i]=(new Coord3d(x,y,z));
                  i=i+1;
                } 
                
        // Create a drawable scatter with a colormap
        MultiColorScatter scatter = new MultiColorScatter( points, new ColorMapper( new ColorMapRainbow(), min, max) );
        // width points
        scatter.setWidth(5);
       
         chart = new Chart();
         //chart = new Chart(Quality.Advanced,"awt");
     
    chart.getAxeLayout().setMainColor(Color.BLACK);
    chart.getView().setBackgroundColor(new Color(246,244,242));
    chart.getScene().add(scatter);
    
    chart.getView().setViewPositionMode(ViewPositionMode.TOP);
    scatter.setLegend( new ColorbarLegend(scatter, chart.getView().getAxe().getLayout(), Color.BLACK, null) );

    scatter.setLegendDisplayed(true);
    Settings.getInstance().setHardwareAccelerated(true);
  
    ChartLauncher.openChart(chart, new Rectangle(0, 0, 800, 800), "Database Map");


            } catch (Exception e) {  
            System.out.println("Retrieving Data Fail");  
            } finally {
                 try {
                 rs.close();
                 pst.close();
                //con.close();
                }
                 catch (Exception e) {
                 }
             } 

  }

  public Chart getChart() {
    return chart;
  }

  protected Chart chart;
    
    

    public static void main(String[] args){
       PlotScatter myChart = new PlotScatter();

       
       
       // Frame frame = new Frame();
        //frame.setSize(750, 750);

//JPanel panel = new JPanel();
//panel.setBorder(javax.swing.BorderFactory.createTitledBorder("Database Map"));
//panel.setBackground(new java.awt.Color(246,244,242));
//panel.setLayout(new java.awt.BorderLayout());

//panel.add((Component)myChart3.getChart().getCanvas());

//contentPane.add((Component)myChart3.getChart().getCanvas().BorderLayout.CENTER);
  //  frame.add((Component)myChart3.getChart().getCanvas());
   
      // frame.add(panel);
     //  frame.setBackground(new java.awt.Color(246,244,242));
       // frame.setVisible(true);
        
  //myChart3.chart.render();
  
    }
}